package co.wangdao.xingshaoye.utils;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则验证类
 */
public class Regular {

    /**
     * 非空验证
     *
     * @return true:空 ，false:非空
     */
    public static boolean IsEmpty(String value) {
        String rex = "\\S+";
        return !matcher(value, rex);
    }

    /**
     * 是否是正整数
     */
    public static boolean IsPositiveInteger(String value) {
        String rex = "^\\d+$";
        return matcher(value, rex);
    }

    /**
     * 验证邮箱地址是否合法
     */
    public static boolean IsVaildEmail(String value) {
        String rex = "^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$";
        return matcher(value, rex);
    }


    /**
     * 验证手机号码是否合法
     */
    public static boolean IsVaildPhone(String value) {
        String rex = "^(1(([358][0-9])|[4][57]|[7][78]))\\d{8}$";
        return matcher(value, rex);
    }


    /**
     * 验证手机号码是否合法,只验证长度和开头为 1
     */
    public static boolean IsVaildPhoneLength(String value) {
        String rex = "^1\\d{10}$";
        return matcher(value, rex);
    }

    /**
     * 正则表达式验证
     *
     * @param value 验证值
     * @param rex   正则规则
     * @return
     */
    public static boolean matcher(String value, String rex) {
        if (TextUtils.isEmpty(value) || TextUtils.isEmpty(rex)) {
            return false;
        }
        Pattern pattern = Pattern.compile(rex);
        Matcher matcher = pattern.matcher(value);
        return matcher.find();
    }
}
